<!--
@fucntion: 教改项目-建设任务
-->
<template>
  <el-row class="teachingreform-construction">
    <el-col style="height:100%;">
      <table-panel v-show="!constructionDlgVisible" :name="'教学改革研究与实践项目-建设任务'" :columns="constructionColumns" :tableData="constructionData" :showRowNumber="false" :multiSelection="true"
      :canCreate="authorities.create" :canEdit="authorities.edit" :canDelete="authorities.delete" :customBtns="customBtns"
      :currentPage="constructionCurrentPage" :totalNum="constructionTotalNum" @page-change="onPageChange('construction',$event)" :pageSize="pageSize"
      @table-searching="onSearching('construction',$event)"
      @table-create="onCreate('construction',$event)" @table-edit="onEdit('construction', $event)"
      @table-delete="onDelete('construction',$event)" @table-export="onExport('construction',$event)"
      @selection-change="onSelectionChange('construction',$event)"
      @table-approval="onApproval"
      @table-multidelete="onMultiDel"></table-panel>
      <con-form v-show="constructionDlgVisible" @form-close="onCloseForm"
      :formRecord="constructionRecord"
      :formRules="constructionRules"></con-form>
    </el-col>
  </el-row>
</template>
<script>
import TablePanel from "@/components/basic/TablePanel";
import {
  formModelInitFromTable,
  formRulesInitFromTable
} from "@/utils/formutils.js";
import { trueFalseRenderer ,b, dateRenderer} from "@/utils/tableutils.js";
import Form from "./construction/Form";

//meta info
var construction = "construction"; // 表格名称
var columnsAttr = "Columns"; // 列
var dataAttr = "Data"; // 数据
var selectionAttr = "Selection"; // 选中
var dlgVisibleAttr = "DlgVisible"; // 对话框
var formAttr = "Form";
var recordAttr = "Record"; // 表单模型
var rulesAttr = "Rules"; // 表单验证
var curPageAttr = "CurrentPage";// 分页-当前页
var totalNumAttr = "TotalNum";// 分页-总数
var qParamAttr = "QueryParams";// 查询参数

//
var constructionColumns = [
  {
    prop:"xmsbbh",
    label:"项目申报编号",
    outOfSearch:true,
    hidden:true
  },
  {
    prop:"jsbh",
    label:"项目负责人编号",// 就是申报中的项目主持人 
    hidden:true
  },
  {
    prop:"xmmc",
    label:"项目名称"
  },
  {
    prop:"xmlx",
    label:"项目类型",
    hidden:true
  },
  {
    prop: "xybh",
    label: "项目所在部门编号",
    outOfSearch:true,
    hidden:true
  },
////////////////////////////
  {
    prop:"jsrwbh",
    label:"建设任务编号",
    outOfSearch:true,
    hidden:true
  },
  {
    prop:"xmjb",
    label:"项目级别",
    rules: [{
      required: true,
      message: "必填", 
      trigger: "blur"
    }]
  },
  {
    prop:"xmbh",
    label:"项目编号",// 生成的字段
    rules:[{
      required: true,
      message: "必填",
      trigger: "blur"
    }]
  },
  {
    prop:"lxsj",
    label:"立项时间",
    type:"date",
    default:new Date(),
    renderer:dateRenderer,
    rules:[{
      required: true,
      message: "请选择",
      trigger: "blur"
    }]
  },
  {
    prop:"jxsj",
    label:"结项时间",
    type:"date",
    default:new Date(),
    renderer:dateRenderer,
    rules:[{
      required: true,
      message: "请选择",
      trigger: "blur"
    }]
  },
  {
    prop:"tbrq",
    label:"填报日期",
    type:"date",
    hidden:true,
    rules:[{
      required: true,
      message: "请选择",
      trigger: "blur"
    }]
  },
  {
    prop:"jsmb",
    label:"建设目标",
    hidden:true,
    rules:[{
      required: true,
      max: 5000, 
      message: "必填，不超过5000个字符",
      trigger: "blur"
    }]
  },
  {
    prop:"jdap",
    label:"进度安排",
    hidden:true,
    rules:[{
      required: true,
      max: 5000, 
      message: "必填，不超过5000个字符",
      trigger: "blur"
    }]
  },
  {
    prop:"yqcgycx",
    label:"预期成果与成效",
    hidden:true,
    rules:[{
      required: true,
      max: 5000, 
      message: "必填，不超过5000个字符",
      trigger: "blur"
    }]
  },
  {
    prop:"bztjyzc",
    label:"保障条件与政策",
    hidden:true,
    rules:[{
      required: true,
      max: 300, 
      message: "必填，不超过300个字符",
      trigger: "blur"
    }]
  },
  {
    prop:"zrrcnnr",
    label:"责任人承诺内容",
    hidden:true,
    rules:[{
      required: true,
      max: 200, 
      message: "必填，不超过200个字符",
      trigger: "blur"
    }]
  },
  {
    prop:"zrrcnshrq",
    label:"责任人承诺审核日期",
    hidden:true,
    type:"date",
    rules:[{
      required: true,
      message: "必填",
      trigger: "blur"
    }]
  },
  {
    prop:"xmszbmshyj",
    label:"项目所在部门审核意见",
    hidden:true
  },
  {
    prop:"xmszbmshrq",
    label:"项目所在部门审核日期",
    hidden:true,
    type:"date",
    // rules:[{
    //   required: true,
    //   message: "必填",
    //   trigger: "blur"
    // }]
  },
  {
    prop:"xxglbmshyj",
    label:"学校管理部门审核意见",
    hidden:true,
    // rules:[{
    //   required: true,
    //   message: "必填",
    //   trigger: "blur"
    // }]
  },
  {
    prop:"xxglbmshrq",
    label:"学校管理部门审核日期",
    hidden:true,
    type:"date",
    // rules:[{
    //   required: true,
    //   message: "必填",
    //   trigger: "blur"
    // }]
  },
  {
    prop:"jsrwzt",
    label:"建设任务状态",
    hidden:true,
  },
  {
    prop:"xmjssurl",
    label:"项目建设书URL",
    hidden:true,
  },
  {
    prop: "sjbmzz",
    label: "上级部门资助",
    type:"number",
    default:0,
    hidden:true,
    rules:[{
      required: true,
      message: "必填，请输入合法的金额",
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
      trigger: "blur"
    }]
  },
  {
    prop: "xxzz",
    label: "学校资助",
    type:"number",
    default:0,
    hidden:true,
    rules:[{
      required: true,
      message: "必填，请输入合法的金额",
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
      trigger: "blur"
    }]
  },
  {
    prop: "yxpt",
    label: "院系配套",
    type:"number",
    default:0,
    hidden:true,
    rules:[{
      required: true,
      message: "必填，请输入合法的金额",
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
      trigger: "blur"
    }]
  },
  {
    prop: "zcjf",
    label: "自筹经费",
    type:"number",
    default:0,
    hidden:true,
    rules:[{
      required: true,
      message: "必填，请输入合法的金额",
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
      trigger: "blur"
    }]
  },
  {
    prop: "qtjf",
    label: "其他经费",
    type:"number",
    default:0,
    hidden:true,
    rules:[{
      required: true,
      message: "必填，请输入合法的金额",
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
      trigger: "blur"
    }]
  },
];

export default {
  name:"Construction",
  components: {
    "table-panel": TablePanel,
    "con-form":Form
  },
  methods: {
    onSearching: function(tname, params) {// 查询
      var me = this;
      let url = "/jgxmjsJsjcxx"; // /static/data/construction.json 

      if(url !== "") {
        me[tname+qParamAttr] = {};
        me[tname+qParamAttr] = params;
        if (params.isAccurate) {
          me[tname+qParamAttr] = params.paramPairs;
        } else {
          me[tname+qParamAttr].keywords = params.query;
        }
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              me[tname+dataAttr] = resp.data.results;
              me[tname+totalNumAttr] = resp.data.total;
            }
        });
      }
    },
    onCreate: function(tname) {// 新增
      var me = this;
      me[tname+recordAttr] = formModelInitFromTable(me[tname+columnsAttr]);
      me[tname+recordAttr].sfqy = "1";// 创建时，有效否是选中的

      me[tname+dlgVisibleAttr] = true;

    },
    onEdit: function(tname, row) {// 编辑
      var me = this;
      me[tname + recordAttr]= row;
      console.log("row=",row);
      me[tname + dlgVisibleAttr] = true;
    },
    onDelete: function(tname, row) {// 删除
      var me = this;
      me.$confirm("确定删除这条记录吗?", "提示", {
        type: "warning"
      }).then(() => {
        me.deleteRequest(`/jgxmjsJsjcxx/${row.jsrwbh}`).then((resp)=>{
          if(resp.status == 200) {
            me.$message.success("删除成功!");
            me.onSearching(construction, {});// 重新加载表格数据
          }else{
            me.$message.error("删除失败！");
          }
        });
      }).catch(() => {});
    },
    onExport: function(tname, params) {// 导出
      var me = this;
      me.$message.info("发送导出请求，返回下载路径");
    },
    onSelectionChange: function(tname, data) {// 选中改变
      var me = this;
      // 缓存记录，按照角色编号，查询对应的选中权限节点数组
      me[tname+selectionAttr] = data;
    },
    onConfirm: function(tname) {// 对话框-确认
      var me = this;
      // 验证信息
      var ret;
      me.$refs[tname + formAttr].validate((result, what)=>{
        ret = result;
      });
      if(!ret){
        me.$message.error("表单信息有误");
        return;
      }
      // 发送请求 
      me.postRequest("/static/data/testSuc.json").then((resp)=>{
        if (resp.status == 200) {
          me.$message.success("提交成功！");
          //最后，隐藏对话框
          me[tname + dlgVisibleAttr] = false;
        } else {
          me.$message.error( "服务器发生错误，代码：" + resp.status);
        }
      });
    },
    onCloseForm:function(){
      var me = this;
      me[construction + dlgVisibleAttr] = false;
      me[construction + recordAttr] = formModelInitFromTable(me[construction+columnsAttr]);// 初始化form的model
    },
    onApproval:function(){// 审批
      console.log("审批！");
    },
    onMultiDel:function(){
      let me = this;
      console.log("selected=",me[construction + selectionAttr].length);

      me.$confirm("确定删除这些记录吗?", "提示", {
        type: "danger"
      }).then(() => {
        me[construction + selectionAttr].forEach((item)=>{
          me.deleteRequest(`/jgxmjsJsjcxx/${item.jsrwbh}`).then((resp)=>{
            if(resp.status == 200 && resp.data.success) {
              me.$message.success("删除成功!");
              me.onSearching(construction,{});// 重新加载表格数据
            }else{
              me.$message.error("删除失败！"+resp.data.msg);
            }
          });
        });
        
      }).catch(() => {});

    }
  },
  data() {
    return {
      authorities:{
        create:true,
        delete:true,
        edit:true,
        approval:true,
        multiDelete:true
      },// 权限控制
      [construction + columnsAttr]: constructionColumns, // 表格-列
      [construction + dataAttr]: [], // 表格的数据
      [construction + selectionAttr]: [], // 当前选中
      [construction + dlgVisibleAttr]: false, // 对话框是否可见
      [construction + recordAttr]: {}, // 表单模型
      [construction + rulesAttr]: {}, // 表单验证
      [construction + curPageAttr]:1,// 当前页
      [construction + totalNumAttr]:0,// 总数
      [construction + qParamAttr]:{},// 查询参数
      pageSize:20,// 每页数量
      customBtns:[]
    };
  },
  created: function() {
    var me = this;
    // 添加权限控制
    // me.authorities = me.$route.meta.authorities;
    if(me.authorities.approval){
      me.customBtns.push({
        type:"success",
        text:"审批",
        icon:"el-icon-check",
        eventName:"table-approval",
      });
    }
    if(me.authorities.multiDelete){
      me.customBtns.push({
        type:"danger",
        text:"删除多条",
        icon:"el-icon-check",
        eventName:"table-multidelete",
      });
    }

    // 表单模型初始化
    me[construction+recordAttr] = formModelInitFromTable(me[construction+columnsAttr]);
    // 表单验证初始化
    me[construction+rulesAttr] = formRulesInitFromTable(me[construction+columnsAttr]);

    //
    me.onSearching(construction,{});
  }
}
</script>
<style lang="scss">

.teachingreform-construction{
  height:100%;
}
</style>

